package net.web{   
	
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;  
	
	/** 
	 * Remoting服务 
	 */  
	[Event(name="netStatus", type="flash.events.NetStatusEvent")]  
	[Event(name="ioError", type="flash.events.IOErrorEvent")]  
	[Event(name="securityError", type="flash.events.SecurityErrorEvent")]  
	
	public class Service extends EventDispatcher 
	{  
		private var _gateway : String;  
		private var _nc : NetConnection;  
		private var _destination : String;
		
		/** 
		 * 构建 
		 * @param gateway remoting网关地址 
		 * @param destination remoting目标地址 
		 * @param objectEncoding remoting编码(amf0/amf3) 
		 */  
		public function Service(gateway : String,destination : String,objectEncoding : uint = 3) {  

			_gateway = gateway;  
			_destination = destination;  
			
			_nc = new NetConnection();  
			_nc.client = this;  
			_nc.objectEncoding = objectEncoding;  
			_nc.addEventListener(NetStatusEvent.NET_STATUS, ncHandler);  
			_nc.addEventListener(IOErrorEvent.IO_ERROR, ncHandler);  
			_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ncHandler);  
			_nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, ncHandler);  
			
			if(_gateway && _gateway.length > 0)
				_nc.connect(_gateway);            
		}  
		
		/**
		 *连接 
		 * @param gateWay
		 * 
		 */		
		public function connect(gateWay:String):void
		{
			if(_gateway != gateWay)
			{
				_gateway = gateWay;
				_nc.connect(gateWay);
			}
		}
		
		/** 
		 * public 
		 */  
		/** 
		 * 设置安全证书的用户名和密码 
		 * @param userName 用户名 
		 * @param password 密码 
		 */  
		public function setRemoteCredentials(userName : String,password : String) : void {  
			_nc.addHeader("Credentials", false, { userid : userName, password : password });  
		}  
		
		/**
		 * 设置标头信息
		 */		
		public function setHeaderObj(data:Object):void
		{
			_nc.addHeader("Credentials", false, data);  
		}
		
		/** 
		 * remoting网关地址 
		 */  
		public function get gateway():String{  
			return _gateway;  
		}  
		/** 
		 * remoting目标地址 
		 */  
		public function get destination():String{  
			return _destination;  
		}  
		
		public function set destination(value:String):void {  
			_destination = value;  
		}  
		
		/** 
		 * remoting编码 
		 */  
		public function get objectEncoding():uint{  
			return _nc.objectEncoding;  
		}  
		
		public function set objectEncoding(value:uint):void{  
			_nc.objectEncoding = value;  
		}  
		
		/** 
		 * remoting连接 
		 */  
		public function get connenction():NetConnection{  
			return _nc;  
		}  
		
		public override function toString() : String {  
			return "[Service gateway="+gateway+", destination="+destination+", objectEncoding="+objectEncoding+"]";  
		}  
		
		/** 
		 * private 
		 */  
		private function ncHandler(event : Event) : void {  
			switch(event.type){  
				case AsyncErrorEvent.ASYNC_ERROR:  
					break;  
				default:  
					dispatchEvent(event);  
			}             
		}  
		
		
		/**
		 * 调用方法
		 * @param methodName	方法名
		 * @param parametres	参数
		 * @return 
		 */		
		public function callMethod( methodName:String,resultHandler:Function,faultHandler:Function, parametres:*):void {  
			new Operation(this,methodName,resultHandler,faultHandler).send.apply(null,parametres);  
		}         
		
	}  
}  